#region

using System.Linq;
using SFMLStart.Data;
using TimeDRODPOF.TDComponents;
using VeeTileEngine2012;
using Utils = SFMLStart.Utilities.Utils;

#endregion

namespace TimeDRODPOF.TDLib
{
    public static class TDLMethods
    {
        public static TDCSpecialSquare.SpecialSquareEvent PressurePlateMovement(TDCPressurePlate pressurePlateComponent)
        {
            return (Entity mEntity, int mNextX, int mNextY, bool mNextSuccess, out bool mAbort) =>
                   {
                       mAbort = false;
                       pressurePlateComponent.Trigger();
                       return true;
                   };
        }

        public static void TryTriggerPlate(TDCPressurePlate pressurePlateComponent, Entity result)
        {
            if (result.Field.GetEntitiesByTag(result.X, result.Y, TDLTags.Solid).Any(y => !y.HasTag(TDLTags.DoesNotTriggerPlate)))
                pressurePlateComponent.Trigger();
        }

        public static void AttachBrokenOverlay(TDCRender mRenderComponent)
        {
            mRenderComponent.AddSprite(Assets.Tilesets["brokenoverlaytiles"].GetSprite(Utils.RandomGenerator.GetNextInt(0, 3),
                                                                                       Utils.RandomGenerator.GetNextInt(0, 3),
                                                                                       Assets.GetTexture(@"environment\brokenoverlay")));
        }

        public static void AttachCrackedOverlay(TDCRender mRenderComponent, int mX)
        {
            mRenderComponent.AddSprite(Assets.Tilesets["brokenoverlaytiles"].GetSprite(mX, 0,
                                                                                       Assets.GetTexture(@"environment\crackedoverlay")));
        }

        public static void Break(bool mIsBroken, Entity mEntity)
        {
            if (!mIsBroken) return;
            mEntity.Destroy();
            TDLHelper.PlaySound("SoundBrokenWall");
        }

        public static void Kill(Entity mEntity)
        {
            mEntity.Destroy();
            TDLHelper.PlaySound("SoundKill1");
        }

        public static void Burn(Entity mEntity)
        {
            mEntity.Destroy();
            TDLHelper.PlaySound("SoundKill1");
        }

        public static bool DiagonalSquareMovement(int mNextY, TDCSwitch switchComponent, int mNextX, out bool mAbort)
        {
            mAbort = false;
            return switchComponent.IsOff || (mNextX != 0 && mNextY != 0);
        }

        public static bool OrthogonalSquareMovement(int mNextY, TDCSwitch switchComponent, int mNextX, out bool mAbort)
        {
            mAbort = false;
            return switchComponent.IsOff || (mNextX == 0 || mNextY == 0);
        }

        public static bool TrapdoorOnMoveFrom(Entity result, bool mIsOff, bool mNextSuccess, Entity mEntity, out bool mAbort, bool mIsOnWater, bool mIsBroken)
        {
            mAbort = false;

            if (mEntity.HasTag(TDLTags.WeightHigh) || (mIsBroken && mEntity.HasTag(TDLTags.WeightLow)))
                if (!mIsOff && mNextSuccess)
                {
                    result.Destroy();
                    TDLFactory.Tile = result.Field.GetTile(result.X, result.Y);
                    result.Field.AddEntity(!mIsOnWater ? TDLFactory.Pit() : TDLFactory.Water());
                    TDLHelper.PlaySound("SoundTrapdoor");
                }

            return true;
        }

        public static void RoachQueenBehavior(int mSpawnTurns, Entity result, ref int turnsToSpawn)
        {
            turnsToSpawn--;

            if (turnsToSpawn != 0) return;
            turnsToSpawn = mSpawnTurns;

            for (var iY = -1; iY < 2; iY++)
                for (var iX = -1; iX < 2; iX++)
                    if (TDLTags.GroundAllowedTags.Any(x => result.Field.HasEntityByTag(result.X + iX, result.Y + iY, x)))
                        if (!TDLTags.GroundObstacleTags.Any(x => result.Field.HasEntityByTag(result.X + iX, result.Y + iY, x)))
                            if (!result.Field.HasEntityByTag(result.X + iX, result.Y + iY, TDLTags.PreventSpawn))
                            {
                                TDLFactory.Tile = result.Field.GetTile(result.X + iX, result.Y + iY);
                                result.Field.AddEntity(TDLFactory.RoachEgg());
                            }
        }

        public static bool ArrowMovement(int mNextX, int mNextY, TDCDirection directionComponent, TDCSwitch switchComponent, out bool mAbort)
        {
            mAbort = false;
            if (switchComponent.IsOff) return true;

            var xy = directionComponent.DirectionVector;

            if (xy.X == 0 || xy.Y == 0) return !((mNextX != xy.X && mNextX == -xy.X) || (mNextY != xy.Y && mNextY == -xy.Y));
            return !((mNextX == 0 || mNextX == -xy.X) && (mNextY == 0 || mNextY == -xy.Y));
        }

        public static bool WeaponMove(Entity mEntity, int mX, int mY)
        {
            if (mEntity.Move(mX, mY)) return true;

            mEntity.IsOutOfField = true;
            mEntity.GetComponent<TDCWeapon>().IsOutOfBounds = true;
            return false;
        }
    }
}